// SPA_Main.cpp :	General interface that generate choice system.
//					1) Get path to source code and invoke parser with it to start parsing.
//					2) Return from parser Succeed ? Invoke query UI : exit
// Author: Heng Belson
//

#include "SPA Front End/Parser2.h"
#include "PKB/DE/DesignExtractor.h"
#include "QueryProjector.h"

int main() {
	STATUS status = STATUS_Not_Processed;
	STRING source(""), choice("-1"), query(""), declaration("");
	cout << "\t\t Welcome to Static Program Analyzer (SPA)\n" << endl;
	cout << "\t\t\t   [by deBuggers, 2011]\n\n" << endl;

	while (atoi(choice.c_str()) != 0) 
	{
		cout << "****************************************************************\n\n" << endl;
		cout << "1.	Analyze Source Code" << "\n" << endl;
		cout << "2.	Select Query" << "\n" << endl;
		cout << "0.	Quit" << "\n\n" << endl;
		cout << "****************************************************************\n\n" << endl;
		cout << "Your choice: ";
		getline(cin, choice);

		if (atoi(choice.c_str()) == 1) 
		{
			cout << "\n\t\t\t 1. Analyze Source Code\n" << endl;
			cout << "\nPlease enter location of your source file: <0 to quit> \n" << endl;
			getline(cin, source);

			if (source.compare("0") == 0) { goto quit; }

			cout << "\n\t[!] " << "Start parsing " << source << "\n\n" << endl;

			Parser2 p = Parser2();
			status = p.parse(source);

			DesignExtractor de = DesignExtractor();
			de.fillTable();

			//VarTable *vt = VarTable::getInstance();
			//cout << "VarTable :: " << endl;
			//for (INTEGER i = 0; i < vt->getTotalSize(); i++)
			//	cout << " " << vt->getVarName(i);
			//cout << "\n" << endl;

			//ProcTable *pt = ProcTable::getInstance();
			//cout << "ProcTable :: " << endl;
			//for (INTEGER i = 0; i < pt->getSize(); i++)
			//	cout << " " << pt->getProcName(i);
			//cout << "\n" << endl;

			cout << "\n\t[!] " << source << " parsed successfully.\n\n" << endl;
		}
		else if (atoi(choice.c_str()) == 2) {
			switch (status) {
			case STATUS_Success: 
				{
					/***********************************************************************************QUERY*/
					cout << "\n\t\t\t 2. Select Query\n" << endl;
					QueryProjector qp = QueryProjector();
					qp.run();
					/*******************************************************************************END QUERY*/
					break;
				}
			case STATUS_Failure:
				cerr << "The system is unable to process the given source code. Please try again." << endl;
				break;
			case STATUS_Not_Processed:
				cerr << "\n\t[!] Please provide source code for analysis before querying.\n" << endl;
				break;
			default:
				cerr << "Unknown STATUS: " << status << endl;
			}
		}
		else if (atoi(choice.c_str()) == 0) break;
		else cout << "\n\t\tInvalid choice\n" << endl;
	}
	quit:
		cout << "\n\t[!] SPA is quitting . . .\n" << endl;
	system("PAUSE");
	return 0;
}